part of '../client.dart';

/// 正常的client用于锁屏请求，而静默client专门用于后台请求

final Argument<Dio> argClient = Argument<Dio>();
final Argument<Dio> argClientSilent = Argument<Dio>();

initClient(String baseUrl) {
  // 加载基础url和拦截器
  argClient.value = createDefaultDio(
    options: BaseOptions(baseUrl: baseUrl),
    interceptors: [_lockScreen],
  );
  argClientSilent.value = createDefaultDio(
    options: BaseOptions(baseUrl: baseUrl),
  );

  // 如果有token的话，则设置Header
  final String? token = tokenRead();
  if (token != null) setTokenHeader(token);
}

extension SetToken on Dio {
  _setToken(String token) => options.headers["Authorization"] = token;
}

setTokenHeader(String token) {
  argClient.value._setToken(token);
  argClientSilent.value._setToken(token);
}
